/*     */ package net.sf.navigator.taglib;
/*     */ 
/*     */ import com.yc.dao.SystemDAO;
/*     */ import com.yc.dao.model.SysmMenuBO;
/*     */ import java.io.IOException;
/*     */ import java.net.MalformedURLException;
/*     */ import java.util.Iterator;
/*     */ import java.util.LinkedList;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.ResourceBundle;
/*     */ import java.util.Set;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import javax.servlet.jsp.JspException;
/*     */ import javax.servlet.jsp.JspWriter;
/*     */ import javax.servlet.jsp.PageContext;
/*     */ import javax.servlet.jsp.tagext.TagSupport;
/*     */ import net.sf.navigator.displayer.MenuDisplayer;
/*     */ import net.sf.navigator.menu.MenuComponent;
/*     */ import net.sf.navigator.menu.MenuRepository;
/*     */ import org.apache.commons.collections.map.LinkedMap;
/*     */ import org.apache.commons.logging.Log;
/*     */ import org.apache.commons.logging.LogFactory;
/*     */ import org.apache.struts.taglib.TagUtils;
/*     */ 
/*     */ public class DisplayMenuTag extends TagSupport
/*     */ {
/*     */   private SystemDAO systemDAO;
/*     */   private MenuRepository repository;
/*     */   private String strUserID;
/*  78 */   protected static final Log log = LogFactory.getLog(DisplayMenuTag.class);
/*     */   private String target;
/*     */   protected String[] aryMenu;
/*     */ 
/*     */   public String getStrUserID()
/*     */   {
/*  67 */     return this.strUserID;
/*     */   }
/*     */ 
/*     */   public void setStrUserID(String strUserID) {
/*  71 */     this.strUserID = strUserID;
/*     */   }
/*     */ 
/*     */   private void setAryMenu(String[] aryMenu) {
/*  75 */     this.aryMenu = aryMenu;
/*     */   }
/*     */ 
/*     */   public void setTarget(String target)
/*     */   {
/*  83 */     this.target = target;
/*     */   }
/*     */ 
/*     */   public int doStartTag() throws JspException {
/*  87 */     MenuDisplayer displayer = 
/*  88 */       (MenuDisplayer)this.pageContext.getAttribute("net.sf.navigator.taglib.DISPLAYER");
/*     */ 
/*  90 */     if (displayer == null) {
/*  91 */       throw new JspException("Could not retrieve the menu displayer.");
/*     */     }
/*     */ 
/*  95 */     this.repository = ((MenuRepository)this.pageContext.getAttribute("net.sf.navigator.repositoryKey"));
/*     */ 
/*  97 */     if (this.repository == null) {
/*  98 */       throw new JspException("Could not obtain the menu repository");
/*     */     }
/* 100 */     this.systemDAO = new SystemDAO();
/*     */ 
/* 102 */     List menuList = this.systemDAO.getAllMenuByUserID(this.strUserID, "0");
/* 103 */     initMenuMap(menuList);
/*     */ 
/* 105 */     for (int i = 0; i < this.aryMenu.length; i++)
/*     */     {
/* 107 */       MenuComponent menu = this.repository.getMenu(this.aryMenu[i]);
/* 108 */       if (menu != null)
/*     */       {
/*     */         try {
/* 111 */           if (this.target != null) {
/* 112 */             displayer.setTarget(this.target);
/*     */           }
/*     */ 
/*     */           try
/*     */           {
/* 119 */             setPageLocation(menu);
/*     */           } catch (MalformedURLException m) {
/* 121 */             log.error("Incorrect action or forward: " + m.getMessage());
/* 122 */             log.warn("Menu '" + menu.getName() + "' location set to #");
/* 123 */             menu.setLocation("#");
/*     */           }
/*     */ 
/* 126 */           displayer.display(menu);
/* 127 */           displayer.setTarget(null);
/*     */         }
/*     */         catch (Exception e) {
/* 130 */           e.printStackTrace();
/* 131 */           throw new JspException(e);
/*     */         }
/*     */       } else {
/* 134 */         String error = UseMenuDisplayerTag.messages.getString("menu.not.found") + 
/* 135 */           " " + this.aryMenu[i];
/* 136 */         log.warn(error);
/*     */         try {
/* 138 */           this.pageContext.getOut().write(error);
/*     */         } catch (IOException io) {
/* 140 */           throw new JspException(error);
/*     */         }
/*     */       }
/*     */     }
/* 144 */     return 0;
/*     */   }
/*     */ 
/*     */   private void initMenu(Map menuMap)
/*     */   {
/* 149 */     if (menuMap != null)
/*     */     {
/* 151 */       Iterator key = menuMap.keySet().iterator();
/* 152 */       while (key.hasNext())
/*     */       {
/* 154 */         String str = key.next().toString();
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   private void initMenuMap(List list)
/*     */   {
/* 161 */     LinkedMap menuMap = new LinkedMap();
/* 162 */     List menuList = new LinkedList();
/* 163 */     this.repository.removeAllMenus();
/* 164 */     for (int i = 0; i < list.size(); i++)
/*     */     {
/* 166 */       SysmMenuBO bo = (SysmMenuBO)list.get(i);
/* 167 */       MenuComponent menu = new MenuComponent();
/* 168 */       menu.setName(bo.getNAME());
/* 169 */       menu.setLocation(bo.getLINK());
/* 170 */       menu.setTitle(bo.getTIP());
/* 171 */       if ("0".equalsIgnoreCase(bo.getPARENT_ID().trim()))
/*     */       {
/* 173 */         this.repository.addMenu(menu);
/* 174 */         menuMap.put(bo.getID(), menu);
/* 175 */         menuList.add(bo.getNAME());
/*     */       }
/*     */       else
/*     */       {
/*     */         try
/*     */         {
/* 181 */           MenuComponent parentMenu = (MenuComponent)menuMap.get(bo.getPARENT_ID());
/* 182 */           List childList = parentMenu.getComponents();
/* 183 */           log.debug(childList);
/* 184 */           if (parentMenu.getComponents().size() == 0)
/*     */           {
/* 186 */             parentMenu.setLocation(bo.getLINK());
/* 187 */             log.debug(bo.getLINK());
/*     */           }
/* 189 */           parentMenu.addMenuComponent(menu);
/*     */         }
/*     */         catch (Exception localException)
/*     */         {
/*     */         }
/*     */       }
/*     */     }
/*     */ 
/* 197 */     String[] menu = new String[menuList.size()];
/* 198 */     for (int i = 0; i < menuList.size(); i++) {
/* 199 */       menu[i] = menuList.get(i).toString();
/*     */     }
/* 201 */     setAryMenu(menu);
/*     */   }
/*     */ 
/*     */   protected void setPageLocation(MenuComponent menu)
/*     */     throws MalformedURLException, JspException
/*     */   {
/* 223 */     HttpServletRequest request = (HttpServletRequest)this.pageContext.getRequest();
/* 224 */     setLocation(menu);
/* 225 */     String url = menu.getLocation();
/*     */ 
/* 228 */     if ((url != null) && (url.indexOf("${") > -1)) {
/* 229 */       String queryString = null;
/*     */ 
/* 231 */       if (url.indexOf("?") > -1) {
/* 232 */         queryString = url.substring(url.indexOf("?") + 1);
/* 233 */         url = url.substring(0, url.indexOf(queryString));
/*     */       }
/*     */ 
/* 237 */       if (queryString != null) {
/* 238 */         menu.setUrl(url + parseString(queryString, request));
/*     */       }
/*     */       else
/* 241 */         menu.setUrl(parseString(url, request).toString());
/*     */     }
/*     */     else {
/* 244 */       menu.setUrl(url);
/*     */     }
/*     */ 
/* 247 */     HttpServletResponse response = (HttpServletResponse)this.pageContext.getResponse();
/* 248 */     if (menu.getUrl() != null) {
/* 249 */       menu.setUrl(response.encodeURL(menu.getUrl()));
/*     */     }
/*     */ 
/* 253 */     MenuComponent[] subMenus = menu.getMenuComponents();
/*     */ 
/* 255 */     if (subMenus.length > 0)
/* 256 */       for (int i = 0; i < subMenus.length; i++)
/* 257 */         setPageLocation(subMenus[i]);
/*     */   }
/*     */ 
/*     */   protected void setLocation(MenuComponent menu)
/*     */     throws MalformedURLException
/*     */   {
/* 265 */     if (menu.getLocation() == null)
/*     */       try {
/* 267 */         if (menu.getPage() != null) {
/* 268 */           HttpServletRequest request = (HttpServletRequest)this.pageContext.getRequest();
/* 269 */           menu.setLocation(request.getContextPath() + getPage(menu.getPage()));
/* 270 */           HttpServletResponse response = (HttpServletResponse)this.pageContext.getResponse();
/* 271 */           menu.setLocation(response.encodeURL(menu.getLocation()));
/* 272 */         } else if (menu.getForward() != null) {
/* 273 */           menu.setLocation(TagUtils.getInstance()
/* 274 */             .computeURL(this.pageContext, menu.getForward(), 
/* 274 */             null, null, null, menu.getModule(), null, null, false));
/* 275 */         } else if (menu.getAction() != null)
/*     */         {
/* 280 */           menu.setLocation(TagUtils.getInstance()
/* 281 */             .computeURL(this.pageContext, null, null, null, 
/* 281 */             menu.getAction(), menu.getModule(), null, null, false));
/*     */         }
/*     */       } catch (NoClassDefFoundError e) {
/* 284 */         if (menu.getForward() != null)
/* 285 */           throw new MalformedURLException("forward '" + menu.getForward() + "' invalid - no struts.jar");
/* 286 */         if (menu.getAction() != null)
/* 287 */           throw new MalformedURLException("action '" + menu.getAction() + "' invalid - no struts.jar");
/*     */       }
/*     */   }
/*     */ 
/*     */   protected String getPage(String page)
/*     */   {
/* 300 */     if (page.startsWith("/")) {
/* 301 */       return page;
/*     */     }
/* 303 */     page = "/" + page;
/*     */ 
/* 306 */     return page;
/*     */   }
/*     */ 
/*     */   private StringBuffer parseString(String str, HttpServletRequest request) {
/* 310 */     StringBuffer sb = new StringBuffer();
/*     */ 
/* 312 */     while (str.indexOf("${") >= 0) {
/* 313 */       sb.append(str.substring(0, str.indexOf("${")));
/*     */ 
/* 315 */       String variable = str.substring(str.indexOf("${") + 2, str.indexOf("}"));
/* 316 */       String value = String.valueOf(this.pageContext.findAttribute(variable));
/*     */ 
/* 318 */       if (value == null)
/*     */       {
/* 320 */         value = request.getParameter(variable);
/*     */       }
/*     */ 
/* 324 */       if (value == null) {
/* 325 */         log.warn("Value for '" + variable + 
/* 326 */           "' not found in pageContext or as a request parameter");
/*     */       }
/*     */ 
/* 329 */       sb.append(value);
/* 330 */       str = str.substring(str.indexOf("}") + 1, str.length());
/*     */     }
/*     */ 
/* 333 */     return sb.append(str);
/*     */   }
/*     */ 
/*     */   public void release() {
/* 337 */     this.aryMenu = null;
/* 338 */     this.target = null;
/*     */   }
/*     */ }

/* Location:           E:\java\apache-tomcat-6.0.36\webapps\btsm\WEB-INF\classes\
 * Qualified Name:     net.sf.navigator.taglib.DisplayMenuTag
 * JD-Core Version:    0.6.2
 */